import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';

const app = express();
const PORT = 3001;

// 代理配置
const proxies = {
  '/legacy': 'http://localhost:3000',      // 老应用
  '/new': 'http://localhost:5001',         // 微前端主应用
  '/micro-auth': 'http://localhost:5002',  // 认证微应用
  '/micro-dashboard': 'http://localhost:5003', // 仪表板微应用
  '/micro-user': 'http://localhost:5004',  // 用户管理微应用
  '/api': 'http://localhost:8080'          // API服务器
};

// 设置代理中间件
Object.entries(proxies).forEach(([path, target]) => {
  app.use(path, createProxyMiddleware({
    target,
    changeOrigin: true,
    logLevel: 'debug',
    onProxyReq: (proxyReq, req, res) => {
      console.log(`[Proxy] ${req.method} ${req.url} -> ${target}`);
    }
  }));
});

app.listen(PORT, () => {
  console.log(`🚀 Development proxy server running on http://localhost:${PORT}`);
  console.log('📋 Available proxies:');
  Object.entries(proxies).forEach(([path, target]) => {
    console.log(`   ${path} -> ${target}`);
  });
});